package com.java.Servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet生命周期
 */
@WebServlet("/life-cycle")
public class LifeCycle extends HttpServlet {
    {
        System.out.println("构造代码块");
    }

    public LifeCycle() {
        System.out.println("构造函数");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("init");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("service");
        super.service(req, res); // 根据请求方法确定下一步调用的函数
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
        super.destroy();
    }

    /**
     * 发送第一次请求时输出结果为：构造代码块 构造函数 init service doGet
     * 发送第二次请求时输出结果为：service doGet
     */
}
